package com.linchong.concurrency.chapter6;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author:linchong
 * @CreateTime:2020-01-04 08:48
 * @Description:测试interrupted
 */
@Slf4j
public class ThreadInterrupt {
	private static final Object MONITOR = new Object();

	public static void main(String[] args) {
		/*Thread t = new Thread() {
			@Override
			public void run() {
				while (true) {
					synchronized (MONITOR) {
						try {
							*//*Thread.sleep(10);*//*
							MONITOR.wait(10);
						} catch (InterruptedException e) {
							log.info("get interrupted info...");
							e.printStackTrace();
						}
					}
				}
			}
		};

		t.start();
		Thread.sleep(100);
		log.info("isInterrupted:{}", t.isInterrupted());
		t.interrupt();
		log.info("isInterrupted:{}", t.isInterrupted());
		t.stop();*/

		/*Thread t = new Thread() {
			@Override
			public void run() {
				while (true) {
					synchronized (MONITOR) {
						try {
							MONITOR.wait(10);
						} catch (InterruptedException e) {
							e.printStackTrace();
							log.info("isInterrupted:{}", Thread.currentThread().isInterrupted());
						}
					}
				}
			}
		};*/

		Thread t = new Thread() {
			@Override
			public void run() {
				while (true) {

				}
			}
		};
		t.start();
		Thread main = Thread.currentThread();
		Thread t2 = new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				main.interrupt();
				log.info("interrupted");
			}
		};
		t2.start();
		try {
			/*
			 * wait()等待t线程
			 * sleep()睡眠t线程
			 * join()操作main线程，所以不会中断t
			 * */
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}


	}
}
